<?php

/**
 * @property CI_Loader $load
 * @property CI_Form_validation $form_validation
 * @property CI_Input $input
 * @property CI_Email $email
 * @property CI_DB_active_record $db
 * @property CI_DB_forge $dbforge
 */
class Catagory extends CI_Controller {

    function __construct() {
        parent::__construct();
        $this->load->model('catagory_model', 'catagory', true);
        $this->catagory->set_table('catagory');
    }

    private function show_page() {
        // $root_id = 72;
        $root_id = $this->catagory->get_rootid();
        $data['tree'] = $this->catagory->get_tree($root_id);
        $data['cat_text'] = $this->load->view('catagory_tree_view', $data, TRUE);

        $data['selName'] = 'sel_catagory';
        $data['cat_select_from'] = $this->load->view('catagory_select_view', $data, TRUE);
        $data['selName'] = 'sel_catagory_to';
        $data['cat_select_to'] = $this->load->view('catagory_select_view', $data, TRUE);
        unset($data['tree']);
        $this->load->view('catagory_view', $data);
    }

    public function index() {
        $operation = $this->input->post('edit_catagory');
        if (isset($operation)) {
            switch ($operation) {
                case '增加类别':
                    $this->add();
                    break;
                case '删除类别':
                    $this->delete();
                    break;
                case '重命名类别':
                    $this->rename();
                    break;
                case '移动':
                    $this->move();
                    break;
                default:
                    break;
            }
        }
        $this->show_page();
    }

    private function delete() {
        $id = $this->input->post('sel_catagory');
        $this->catagory->delete_catagory($id);
    }

    private function add() {
        $id = $this->input->post('sel_catagory');
        $newCat = $this->input->post('txtNewCatagoryName');
        $this->catagory->add_catagory($id, $newCat);
    }

    private function rename() {
        $id = $this->input->post('sel_catagory');
        $newCat = $this->input->post('txtNewCatagoryName');
        $this->catagory->ren_catagory($id, $newCat);
    }

    public function move() {
        $from = $this->input->post('sel_catagory');
        $to = $this->input->post('sel_catagory_to');
        $this->catagory->move_catagory($from,$to);
    }

}

?>